package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.ChannelMerchants;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.ChannelConsumeDetailMapper;
import com.rm.zeyou.mapper.ChannelMerchantsMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.ChannelMerchantsService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.ChannelIndexVo;
import com.rm.zeyou.vo.response.ChannelIndexResponseVo;
import com.rm.zeyou.vo.response.DeteilList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 渠道商表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Service
public class ChannelMerchantsServiceImpl extends ServiceImpl<ChannelMerchantsMapper, ChannelMerchants> implements ChannelMerchantsService {
    @Resource
    private ChannelMerchantsMapper channelMerchantsMapper;
    @Resource
    private ChannelConsumeDetailMapper channelConsumeDetailMapper;

    @Override
    public ApiRestResponse<ChannelIndexResponseVo> channelIndex(User user, ChannelIndexVo requestVo) {
        if (!user.getUserIdentity().equals(NumberConstants.TWO_CONSTANTS)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.NOT_CHANNEL);
        }

        ChannelIndexResponseVo channelIndexResponseVo = channelConsumeDetailMapper.selectTotal(user.getId());
        if (CheckEmptyUtil.isEmpty(channelIndexResponseVo)) {
            channelIndexResponseVo = new ChannelIndexResponseVo();
            channelIndexResponseVo.setChannelTotalPrice(BigDecimal.ZERO);
            channelIndexResponseVo.setChannelTotalCount(NumberConstants.ZERO_CONSTANTS);
            channelIndexResponseVo.setTotalPrice(BigDecimal.ZERO);
        }
        ChannelMerchants channelMerchants = channelMerchantsMapper.selectOne(new LambdaQueryWrapper<ChannelMerchants>()
                .eq(ChannelMerchants::getUserId, user.getId()));
        channelIndexResponseVo.setChannelQrcode(channelMerchants.getChannelMerchantsInvitedQrcode());

        PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize());
        List<DeteilList> deteilLists = channelConsumeDetailMapper.channelOrderDeteilList(user.getId(), requestVo.getKeyWord());
        channelIndexResponseVo.setDeteilList(new PageInfo<>(deteilLists));
        return ApiRestResponse.success(channelIndexResponseVo);
    }
}
